package com.EAIPClub.LiveStreamingManager.service.impl;

import com.EAIPClub.LiveStreamingManager.mapper.LiveStreamingManagerMapper;
import com.EAIPClub.LiveStreamingManager.service.LiveStreamingManagerListService;
import com.api.POJO.LiveStreamingManager.LiveStreamingManagerInfo;
import com.api.POJO.LiveStreamingManager.LiveStreamingManagerListInfo;
import com.api.POJO.LiveStreamingManager.LiveStreamingManagerListReturnMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description :
 * @Author :
 * @Date : 2021/12/10
 */
@Service
@Slf4j
public class LiveStreamingManagerManagerListServiceImpl implements LiveStreamingManagerListService {

    private LiveStreamingManagerMapper mapper ;

    @Autowired
    public void setMapper(LiveStreamingManagerMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public LiveStreamingManagerListReturnMsg addNewLiveStreamingInfo(LiveStreamingManagerListInfo info) {
        LiveStreamingManagerListReturnMsg returnMsg = new LiveStreamingManagerListReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将增加新的直播信息.info:{}",info);
        if (info==null){
            log.warn("参数错误!直播信息为空.");
            returnMsg.setStatus(500);
            Msg.add("直播信息为空!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        LiveStreamingManagerListInfo liveListInfoByToken = mapper.getLiveListInfoByToken(info.getToken());
        if(liveListInfoByToken!=null){
            log.warn("检测到已有直播信息!直播信息:{}",liveListInfoByToken);
            returnMsg.setStatus(200);
            returnMsg.setData(liveListInfoByToken);
            return returnMsg;
        }
        Boolean aBoolean = mapper.addNewLiveListInfo(info);
        if (!aBoolean){
            log.warn("增加新的直播信息失败!");
            returnMsg.setStatus(500);
            Msg.add("增加新的直播信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("增加新的直播信息成功.info:{}",info);
        returnMsg.setStatus(200);
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public LiveStreamingManagerListReturnMsg getAllLiveStreamingList() {
        LiveStreamingManagerListReturnMsg returnMsg = new LiveStreamingManagerListReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将开始获取所有直播列表.");
        List<LiveStreamingManagerListInfo> allLiveListInfo = mapper.getAllLiveListInfo();
        log.info("获取结果为:{}",allLiveListInfo);
        if (allLiveListInfo==null){
            log.warn("获取所有直播列表失败!返回为空.");
            returnMsg.setStatus(500);
            Msg.add("获取所有直播列表失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        returnMsg.setDatas(allLiveListInfo);
        returnMsg.setStatus(200);
        return returnMsg;
    }

    @Override
    public LiveStreamingManagerListReturnMsg getLiveStreamingInfoByToken(String token) {
        LiveStreamingManagerListReturnMsg returnMsg = new LiveStreamingManagerListReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将通过token获取直播信息.token:{}",token);
        if (token ==null || token.equals("")){
            log.warn("检测到token为空!");
            returnMsg.setStatus(500);
            Msg.add("检测到token为空!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        LiveStreamingManagerInfo rtmpLiveStreamingManagerInfoByToken = mapper.getRTMPLiveStreamingInfoByToken(token);
        if (!(rtmpLiveStreamingManagerInfoByToken ==null)){
            log.info("找到该token的rtmp直播地址:{},token:{},即将分割.", rtmpLiveStreamingManagerInfoByToken.getAddress(),token);
            String rtmpaddress = rtmpLiveStreamingManagerInfoByToken.getAddress();
            String[] split = rtmpaddress.split("/");
            String dAddress=split[4];
            log.info("分割结果为:{}",dAddress);
            String liveaddress = "http://115.158.28.160:8090/live/"+dAddress+".flv";
            log.info("合并地址为:{}",liveaddress);
            returnMsg.setStatus(200);
            returnMsg.setAddress(liveaddress);
            return returnMsg;
        }
        LiveStreamingManagerInfo webRtcLiveStreamingManagerInfoByToken = mapper.getWebRtcLiveStreamingInfoByToken(token);
        if (!(webRtcLiveStreamingManagerInfoByToken ==null)){
            log.info("找到该token的webrtc直播地址:{},token:{},即将分割.", webRtcLiveStreamingManagerInfoByToken.getAddress(),token);
            String webrtcAddress = webRtcLiveStreamingManagerInfoByToken.getAddress();
            String[] split = webrtcAddress.split("/");
            String dAddress=split[4];
            log.info("分割结果为:{}",dAddress);
            String liveaddress = "http://115.158.28.160:8090/live/"+dAddress+".flv";
            log.info("合并地址为:{}",liveaddress);
            returnMsg.setStatus(200);
            returnMsg.setAddress(liveaddress);
            return returnMsg;
        }
        log.info("未能找到对应的直播地址!token:{}",token);
        returnMsg.setStatus(500);
        Msg.add("未能找到对应的直播地址!");
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public LiveStreamingManagerListReturnMsg deleteLiveStreamingInfoByToken(String token) {
        LiveStreamingManagerListReturnMsg returnMsg = new LiveStreamingManagerListReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将通过token删除直播");
        Boolean aBoolean = mapper.deleteLiveListInfoByToken(token);
        if (!aBoolean){
            log.warn("通过token删除直播信息失败!token:{}",token);
            returnMsg.setStatus(500);
            Msg.add("通过token删除直播信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("通过token删除直播信息成功!token:{}",token);
        returnMsg.setStatus(200);
        Msg.add("删除直播信息成功!");
        returnMsg.setMsg(Msg);
        return returnMsg;
    }
}
